package com.zqxq.lottery.api.controller;

import com.alipay.api.AlipayApiException;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.DispatcherUtils;
import com.zqxq.lottery.api.feign.LotteryServerFeignClient;
import com.zqxq.lottery.api.service.IWechatService;
import com.zqxq.lottery.entity.po.sys.SysConfigPO;
import com.zqxq.lottery.entity.vo.jczq.JczqMatchVO;
import com.zqxq.lottery.entity.vo.order.UserOrderBetVO;
import com.zqxq.lottery.entity.vo.order.UserOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@Api(value = "微信支付接口",tags = "微信支付接口")
@RestController
@RequestMapping("/wechat")
@Slf4j
public class WechatController {

    @Autowired
    private IWechatService wechatService;
    @Autowired
    private LotteryServerFeignClient lotteryServerFeignClient;

    @PostMapping("/rechargeParams")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "String")
            ,@ApiImplicitParam(name = "money", value = "充值金额", required = true, paramType = "query", dataType = "String")
    })
    @ApiOperation(value = "充值参数",notes = "充值参数")
    public Result rechargeParams(HttpServletRequest request, Long userId, BigDecimal money) throws AlipayApiException {
        if(userId == null || money == null){
            return new Result(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
        }
        Result<SysConfigPO> sysConfigPOResult = lotteryServerFeignClient.queryConfigValue("","RECHARGE_SWITCH");
        SysConfigPO sysConfigPO = sysConfigPOResult.getData();
        if(sysConfigPO!=null && "0".equals(sysConfigPO.getCval())){
            return new Result(10022, "充值功能暂用！");
        }
        /*if(money.doubleValue() < 10){
            return new Result(10023, "最低充值金额10元！");
        }*/
        if(money.doubleValue() > 20000){
            return new Result(10024, "最高充值金额2万元！");
        }
        Result result = new Result();
        result.setData(wechatService.rechargeParams(request, userId, money));
        return result;
    }

    @PostMapping("/consumeParams")
    @ApiOperation(value = "购买参数",notes = "购买参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单Id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "couponId", value = "优惠券Id", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "balance", value = "余额抵扣金额", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "money", value = "还需支付金额", required = false, paramType = "query", dataType = "String")
    })
    public Result consumeParams(HttpServletRequest request, Long orderId, Long couponId, BigDecimal balance, BigDecimal money) throws AlipayApiException {
        Result result = new Result();
        if(orderId == null){
            return new Result(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
        }
        //获取订单金额
        Result<UserOrderVO> order = lotteryServerFeignClient.getUserOrderH5(orderId);
        if(!order.success() || order.getData() == null){
            return order;
        }
        if(!"0".equals(order.getData().getStatus())){
            return new Result(CodeEnum.FAIL_100002.getCode(), "该期竞猜已停售，请重新选号");
        }
        UserOrderVO orderVO = order.getData();
        Long lotteryId = DispatcherUtils.parseLong(orderVO.getLotteryId());
        if(lotteryId==21L){
            //如果是足彩，进行开赛时间的验证
            List<UserOrderBetVO> UserOrderBetList =  orderVO.getOrderBetList();
            for (UserOrderBetVO userOrderBetVO : UserOrderBetList){
                Result<JczqMatchVO> resultJczqMatch = lotteryServerFeignClient.getJczqMatchById((userOrderBetVO.getPeriod()));
                JczqMatchVO jczqMatchVO = resultJczqMatch.getData();
                Long closeTime = Long.valueOf(jczqMatchVO.getCloseTime());
                Long currentTime = System.currentTimeMillis();
                if(closeTime<currentTime){
                    lotteryServerFeignClient.updateUserOrderStatus(String.valueOf(orderId),String.valueOf(7));
                    return new Result(CodeEnum.FAIL_100002.getCode(), "比赛已经开始，不能购买！");
                }
            }

        }
        result.setData(wechatService.consumeParams(request,Long.valueOf(orderVO.getUserId()),orderId,couponId,balance, money));
        return result;
    }
    @PostMapping("/getAccessToken")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "code", required = true, paramType = "query", dataType = "String")
    })
    @ApiOperation(value = "获取授权token",notes = "获取授权token")
    public Result getAccessToken(String code) throws ServletException, IOException {
        Result result = new Result();
        result.setData(wechatService.getAccessToken(code));
        return result;
    }


}
